<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登录名" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入登录名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择租户类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.tenantType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>

        <el-form-item label="租户状态" prop="enabled">
         <el-select v-model="queryParams.enabled" placeholder="请选择租户状态" clearable size="small">
           <el-option
             v-for="dict in dict.type.isEnabled"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
         </el-form-item>
      </el-form-item>
     <!-- <el-form-item label="用户id" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
   <el-form-item label="用户数量限制" prop="userNumLimit">
        <el-input
          v-model="queryParams.userNumLimit"
          placeholder="请输入用户数量限制"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="到期时间" prop="expireTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.expireTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择到期时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tenant:add']"
        >新增</el-button>
      </el-col>

       <el-col :span="2.5">
         <el-button plain  size="mini" type="primary" :disabled="multiple" icon="el-icon-success"  @click="handleEnable(0)">启用</el-button>
         <el-button plain  size="mini" type="danger" :disabled="multiple" icon="el-icon-error"  @click="handleEnable(1)">禁用</el-button>
      </el-col>
  <!--   <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tenant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tenant:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" max-height="620px" :border="true" :data="tenantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" v-if="false" prop="id" />
     <!-- <el-table-column label="用户id" align="center" prop="tenantId" /> -->
      <el-table-column label="登录名称" width="160" align="center" prop="loginName" />
      <el-table-column label="用户数量限制" width="130"  align="center" prop="userNumLimit" />
      <el-table-column label="租户类型" width="110" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==0"  type="info">免费租户</el-tag>
          <el-tag v-if="scope.row.type==1" type="success">付费租户</el-tag>
         <!-- <dict-tag :options="dict.type.tenantType" :value="scope.row.type"/> -->
        </template>
      </el-table-column>
      <el-table-column label="租户状态" width="110" align="center" prop="enabled" >
        <template slot-scope="scope">
         <!-- <span v-if="scope.row.enabled==1">启用</span>
          <span v-if="scope.row.enabled==0" type="danger">禁用</span> -->
          <el-tag v-if="scope.row.enabled==0">启用</el-tag>
          <el-tag v-if="scope.row.enabled==1" type="danger">禁用</el-tag>
          <el-tag v-if="scope.row.enabled==2" type="warning">过期</el-tag>
           <!--  <dict-tag :options="dict.type.isEnabled" :value="scope.row.enabled"/> -->
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
      <el-table-column label="到期时间" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
             v-if="scope.row.enabled<2"
            v-hasPermi="['system:tenant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-date"
            v-if="scope.row.enabled==2"
            @click="handleContinue(scope.row)"
            v-hasPermi="['system:tenant:edit']"
          >续期</el-button>
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tenant:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="用户id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入用户id" />
        </el-form-item> -->
        <el-form-item label="公司名称" prop="companyName" v-if="form.id ==null">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" >
              </el-input>
        </el-form-item>
        <el-form-item label="登录名" prop="loginName" v-if="form.enabled<2">
          <el-input v-model="form.loginName" placeholder="请输入登录名" >
             <template slot="append">默认密码：123456</template>
              </el-input>
        </el-form-item>
        <el-form-item label="用户数量限制" prop="userNumLimit">
          <el-input-number v-model="form.userNumLimit" :min="1" placeholder="请输入用户数量限制" style="width: 100%;"/>
        </el-form-item>
        <el-form-item label="租户类型" prop="type" v-if="form.enabled<2">
          <el-select v-model="form.type" placeholder="请选择租户类型"  style="width: 100%;">
            <el-option
              v-for="dict in dict.type.tenantType"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="启用 0-禁用  1-启用" prop="enabled">
          <el-input v-model="form.enabled" placeholder="请输入启用 0-禁用  1-启用" />
        </el-form-item> -->
        <el-form-item label="到期时间" prop="expireTime" v-if="form.id ==null || form.enabled ==2">
          <el-date-picker clearable size="small"
            v-model="form.expireTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            :picker-options="pickerOptions"
            placeholder="选择到期时间"  style="width: 100%;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark" v-if="form.enabled<2">
          <el-input  v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { enableTenant,listTenant, getTenant, delTenant, addTenant, updateTenant } from "@/api/system/tenant";

export default {
  name: "Tenant",
  dicts: ['tenantType','isEnabled'],
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户管理表格数据
      tenantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        loginName: null,
        userNumLimit: null,
        type: null,
        enabled: null,
        expireTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // companyName:[
        //   { required: true, message: "公司名称不能为空", trigger: "blur" }
        // ],
        loginName: [
          { required: true, message: "登录名不能为空", trigger: "blur" }
        ],
        expireTime:[
           { required: true, message: "到期时间不能为空", trigger: "change" }
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          const date = new Date();
          date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
          return time.getTime() < date.getTime();
        },
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 续期 */
    handleContinue(row){
      this.reset();
      const id = row.id || this.ids
      getTenant(id).then(response => {
        this.form = response.data;
        this.form.enabled =2;
        const date = new Date();
        date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
        // console.log(this.parseTime(date))
        this.form.expireTime =  this.parseTime(date);
        this.open = true;
        this.title = "续期租户";
      });
    },
    /** 查询租户管理列表 */
    getList() {
      this.loading = true;
      listTenant(this.queryParams).then(response => {
        this.tenantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        loginName: null,
        userNumLimit: null,
        type: null,
        enabled: null,
        createTime: null,
        expireTime: null,
        remark: null,
        companyName:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTenant(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTenant(this.form).then(response => {
              if(this.form.enabled==2){
                 this.$modal.msgSuccess("续期成功");
              }else{
                 this.$modal.msgSuccess("修改成功");
              }
              this.open = false;
              this.getList();
            });
          } else {
            addTenant(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除租户管理编号为"' + ids + '"的数据项？').then(function() {
        return delTenant(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },handleEnable(enable){
      let msg="启用";
      if(enable==1){
        msg="禁用";

      }
       const ids=this.ids;
       this.$modal.confirm('是否确认 '+msg+' 租户管理编号为"' + ids + '"的数据项？').then(function() {
         return enableTenant(ids,enable);
       }).then(() => {
         this.getList();
         this.$modal.msgSuccess(msg+"成功");
       }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/tenant/export', {
        ...this.queryParams
      }, `tenant_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
